package com.lanchetech.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@ApiModel(value = "com.lanchetech.entity.User")
@JsonIgnoreProperties({"handler"})
@Data
public class User implements UserDetails, Serializable {
    /**
     * 自增id
     */
    @ApiModelProperty(value = "自增id")
    private Long id;

    /**
     * 微信openid
     */
    @ApiModelProperty(value = "微信openid")
    private String openid;

    /**
     * 用户手机号作为用户名
     */
    @ApiModelProperty(value = "用户手机号作为用户名")
    private String username;

    /**
     * 密码
     */
    @ApiModelProperty(value = "密码")
    private String password;

    /**
     * 昵称
     */
    @ApiModelProperty(value = "昵称")
    private String nickname;

    /**
     * 头像
     */
    @ApiModelProperty(value = "头像")
    private String avatar;

    /**
     * 金币
     */
    @ApiModelProperty(value = "金币")
    private BigDecimal coin;

    /**
     * 余额
     */
    @ApiModelProperty(value = "余额")
    private BigDecimal point;

    /**
     * 经验
     */
    @ApiModelProperty(value = "经验")
    private Integer exp;

    /**
     * 等级
     */
    @ApiModelProperty(value = "等级")
    private Integer level;

    /**
     * 最近一次修改密码时间
     */
    @ApiModelProperty(value = "最近一次修改密码时间")
    private Date lastPasswordResetDate;

    /**
     * 用户状态 0 未知 1 正常
     */
    @ApiModelProperty(value = "用户状态 0 未知 1 正常")
    private Integer status;

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间")
    private Date createdAt;

    /**
     * 更新时间
     */
    @ApiModelProperty(value = "更新时间")
    private Date updatedAt;

    /**
     * 嗨币
     */
    @ApiModelProperty(value = "嗨币")
    private BigDecimal cardPoint;

    /**
     * 广告余额
     */
    @ApiModelProperty(value = "广告余额")
    private BigDecimal adPoint;

    /**
     * 商家编号
     */
    @ApiModelProperty(value = "商家编号")
    private Long shopId;

    /**
     * 安全密码
     */
    @ApiModelProperty(value = "安全密码")
    private String payPassword;

    /**
     * 注册渠道 0 小程序 1 APP
     */
    @ApiModelProperty(value = "注册渠道 0 小程序 1 APP")
    private Byte fromChannel;

    /**
     * APP注册时的openid
     */
    @ApiModelProperty(value = "APP注册时的openid")
    private String appOpenid;

    /**
     * APP推送编号
     */
    @ApiModelProperty(value = "APP推送编号")
    private String registerId;

    /**
     * 员工类型 0 非员工 1 员工
     */
    @ApiModelProperty(value = "员工类型 0 非员工 1 员工")
    private Byte staffType;

    /**
     * 代理身份折扣比例
     */
    private BigDecimal agencyDiscount;

    /**
     * 代理身份 0 非代理 1 代理
     */
    @ApiModelProperty(value = "代理身份 0 非代理 1 代理")
    private String agency;

    private static final long serialVersionUID = 1L;

    private List<Role> roles;

    public Date getLastPasswordResetDate() {
        return lastPasswordResetDate;
    }

    public void setLastPasswordResetDate(Date lastPasswordResetDate) {
        this.lastPasswordResetDate = lastPasswordResetDate;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() { //2
        List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
        List<Role> roles = this.getRoles();
        if (roles == null) {
            return auths;
        }
        for (Role role : roles) {
            auths.add(new SimpleGrantedAuthority(role.getName()));
        }
        return auths;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}